package v1

import (
	"team-evaluate/global"
	"team-evaluate/internal/model"
	"team-evaluate/internal/service"
	"team-evaluate/pkg/app"
	"team-evaluate/pkg/convert"
	"team-evaluate/pkg/errcode"
	"team-evaluate/pkg/util"

	"github.com/gin-gonic/gin"
)

type UserTeam struct{}

func NewUserTeam() UserTeam {
	return UserTeam{}
}

// @Summary 用户发出加入某个团队申请
// @Produce  json
// @Tags 用户团队关系管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param user_id body int true "用户 ID"
// @Param team_id body int true "团队 ID"
// @Param user_msg body string false "个人描述"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user [post]
func (ut UserTeam) Create(c *gin.Context) {
	param := model.CreateUserTeamRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.CreateUserTeam(&param)
	if err != nil {
		global.Logger.Errorf("svc.CreateUserTeam err: %v", err)
		response.ToErrorResponse(errcode.ErrorCreateUserTeamFail.WithDetails(err.Error()))
		return
	}
	response.ToResponse(nil)
	return
}

// @Summary 获取团队中用户列表
// @Produce  json
// @Tags 用户团队关系管理
// @Param Authorization header string true "Bearer 用户令牌"
// @param team_id query int true "团队id"
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user-teams/users [get]
func (ut UserTeam) UserList(c *gin.Context) {
	param := model.UserTeamUserListRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err1 := util.StrutToString(param)
	if err1 == nil {
		c.Set("request_params", p)
	}

	pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
	svc := service.New(c.Request.Context())

	totalRows, list, err := svc.GetUserTeamUserList(param.TeamId, &pager)
	if err != nil {
		response.ToErrorResponse(err)
		return
	}

	response.ToResponseList(list, int(totalRows))
}

// @Summary 获取团队列表
// @Produce  json
// @Tags 用户团队关系管理
// @Param Authorization header string true "Bearer 用户令牌"
// @param user_id path int true "用户id"
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user-teams/teams [get]
func (ut UserTeam) TeamList(c *gin.Context) {
	param := model.UserTeamTeamListRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err1 := util.StrutToString(param)
	if err1 == nil {
		c.Set("request_params", p)
	}

	pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
	svc := service.New(c.Request.Context())

	totalRows, list, err := svc.GetUserTeamTeamList(param.UserId, &pager)
	if err != nil {
		response.ToErrorResponse(err)
		return
	}

	response.ToResponseList(list, int(totalRows))
	return
}

// @Summary 根据团队名查找团队列表
// @Produce  json
// @Tags 用户团队关系管理
// @Param Authorization header string true "Bearer 用户令牌"
// @param user_id path int true "用户id"
// @param team_name query string true "团队名称"
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user-teams/teams-byname [get]
func (ut UserTeam) TeamListByTeamName(c *gin.Context) {
	param := model.UserTeamTeamListByTeamNameRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err1 := util.StrutToString(param)
	if err1 == nil {
		c.Set("request_params", p)
	}

	pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
	svc := service.New(c.Request.Context())

	totalRows, list, err := svc.GetUTTeamListByTN(&param, &pager)
	if err != nil {
		response.ToErrorResponse(err)
		return
	}

	response.ToResponseList(list, int(totalRows))
	return
}

// @Summary 同意用户加入自己团队
// @Produce  json
// @Tags 用户团队关系管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id path int true "用户团队关系 ID"
// @Param name body int true "用户 ID"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user-team/agree/{id} [put]
func (ut UserTeam) Agree(c *gin.Context) {
	param := model.UserTeamAgreeRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.UserTeamAgree(&param)
	if err != nil {
		global.Logger.Errorf("svc.UserTeamAgree err: %v", err)
		response.ToErrorResponse(errcode.ErrorUserTeamAgreeFail.WithDetails(err.Error()))
		return
	}
	response.ToResponse(nil)
	return
}

// @Summary 删除用户加入团队请求
// @Produce  json
// @Tags 用户团队关系管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id path int true "用户团队关系 ID"
// @Param name body int true "用户 ID"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user-team/{id} [delete]
func (ut UserTeam) Delete(c *gin.Context) {
	param := model.UserTeamRefuseRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.DeleteUserTeam(&param)
	if err != nil {
		global.Logger.Errorf("svc.UserTeamRefuse err: %v", err)
		response.ToErrorResponse(errcode.ErrorUserTeamRefuseFail.WithDetails(err.Error()))
		return
	}
	response.ToResponse(nil)
	return
}

// @Summary 用户从团队离开
// @Produce  json
// @Tags 用户团队关系管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param user_id body int true "用户 ID"
// @Param team_id body int true "团队 ID"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user-team/leavel [delete]
func (ut UserTeam) LeavelTeam(c *gin.Context) {
	param := model.UserLeaveTeamRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.UserLeaveTeam(&param)
	if err != nil {
		global.Logger.Errorf("svc.UserLeaveTeam err: %v", err)
		response.ToErrorResponse(errcode.ErrorUserLeaveTeamFail.WithDetails(err.Error()))
		return
	}
	response.ToResponse(nil)
	return
}
